package czk.concrent.threadpool.rejectpolicy;

import czk.concrent.threadpool.dymicthreadpool.MyLinkedBlockingQueue;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author chenzhangkun
 * @ClassName CallUnnerPolicyDemo
 * @description: TODO
 * @date 2024年12月26日
 */
public class DiscardPolicyDemo {

	public static void main(String[] args) {
		// 调用线程池
		ThreadPoolExecutor callRunnerPool = new ThreadPoolExecutor(2,
				5,
				100L,
				TimeUnit.SECONDS,
				new MyLinkedBlockingQueue<>(3),

				r -> {
					SecurityManager s = System.getSecurityManager();
					// 哪个线程组
					ThreadGroup threadGroup = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
					String namePrefix = "call-";
					return new Thread(threadGroup, r, namePrefix, 0);
				},
				new ThreadPoolExecutor.DiscardPolicy());
		// 被调用线程池
		ThreadPoolExecutor runPool = new ThreadPoolExecutor(1,
				1,
				100L,
				TimeUnit.SECONDS,
				new MyLinkedBlockingQueue<>(1),
				r -> {
					SecurityManager s = System.getSecurityManager();
					// 哪个线程组
					ThreadGroup threadGroup = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
					String namePrefix = "run-";
					return new Thread(threadGroup, r, namePrefix, 0);
				},
				new ThreadPoolExecutor.DiscardPolicy());
		for (int i = 0; i < 10; i++) {

			int finalI = i;
			callRunnerPool.execute(() -> {

				runPool.execute(() -> {

					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						throw new RuntimeException(e);
					}
					// 执行任务的线程名称run-====0
					// 执行任务的线程名称run-====5
					// 超过的任务被丢掉了
					System.out.println("执行任务的线程名称" + Thread.currentThread().getName() + "====" + finalI);

				});


			});

		}
		while (true) {
		}

	}

}
